package com.jt.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.jt.pojo.Cart;
import com.jt.pojo.Order;
import com.jt.service.DubboCartService;
import com.jt.service.DubboOrderService;
import com.jt.util.UserThreadLocal;
import com.jt.vo.SysResult;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * @author Cx
 * @stage JT Of Fourth stage
 * @creat 2020-12-17 14:09
 */
@Controller
@RequestMapping("/order/")
public class OrderController {

    @Reference(check = false)
    private DubboCartService cartService;
    @Reference(check = false)
    private DubboOrderService orderService;

    // 实现订单确认页面跳转
    @RequestMapping("create")
    public String orderCart(Model model) {
        long userId = UserThreadLocal.getUser().getId();
        List<Cart> cartList = cartService.findCartListByUserId(userId);
        model.addAttribute("carts", cartList);
        return "order-cart";
    }

    // 实现入库
    @RequestMapping("submit")
    @ResponseBody
    public SysResult saveOrder(Order order) {
        long userId = UserThreadLocal.getUser().getId();
        order.setUserId(userId);
        String orderId = orderService.saveOrder(order);
        if (StringUtils.hasLength(orderId))
            return SysResult.success(orderId);
        return SysResult.fail();
    }

    // 实现订单页面查询
    @RequestMapping("success")
    public String success(@RequestParam("id") String orderId,
                          Model model) {
        Order order = orderService.findOrderById(orderId);
        model.addAttribute("order", order);
        return "success";
    }

}
